﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using Stetson.Emulator.Hardware;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
using System.Reflection;

namespace Stetson
{
    public partial class Keyboard : DockContent
    {
        public Keyboard()
        {
            InitializeComponent();
            Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("Stetson.keymap83plus.map");
            BinaryFormatter formatter = new BinaryFormatter();
            KeyMap = (Dictionary<Rectangle, string>)formatter.Deserialize(stream);
            stream.Close();
            //FormClosing += new FormClosingEventHandler(Keyboard_FormClosing);
        }

        //void Keyboard_FormClosing(object sender, FormClosingEventArgs e)
        //{
        //    Stream stream = File.Create("keymap83plus.map");
        //    BinaryFormatter format = new BinaryFormatter();
        //    format.Serialize(stream, KeyMap);
        //    stream.Close();
        //}

        public event EventHandler<EmulatorKeyEventArgs> EmulatorKeyUp, EmulatorKeyDown;

        Dictionary<Rectangle, string> KeyMap = new Dictionary<Rectangle, string>();

        private void pictureBox1_MouseDown(object sender, MouseEventArgs e)
        {
            int x = (int)(((double)e.X / (double)this.Width) * 100d);
            int y = (int)(((double)e.Y / (double)this.Height) * 100d);
            foreach (KeyValuePair<Rectangle, string> pair in KeyMap)
            {
                if (pair.Key.IntersectsWith(new Rectangle(x, y, 1, 1)))
                {
                    if (EmulatorKeyDown != null)
                        EmulatorKeyDown(this, new EmulatorKeyEventArgs(pair.Value));
                    break;
                }
            }
        }

        private void pictureBox1_MouseUp(object sender, MouseEventArgs e)
        {
            int x = (int)(((double)e.X / (double)this.Width) * 100d);
            int y = (int)(((double)e.Y / (double)this.Height) * 100d);
            //TextInput input = new TextInput();
            //input.ShowDialog();
            //KeyMap.Add(new Rectangle(x, y, endx - x, endy - y), input.textBox1.Text);
            foreach (KeyValuePair<Rectangle, string> pair in KeyMap)
            {
                if (pair.Key.IntersectsWith(new Rectangle(x, y, 1, 1)))
                {
                    if (EmulatorKeyUp != null)
                        EmulatorKeyUp(this, new EmulatorKeyEventArgs(pair.Value));
                    break;
                }
            }
        }
    }

    public class EmulatorKeyEventArgs : EventArgs
    {
        public string Keyname;

        public EmulatorKeyEventArgs(string Keyname)
        {
            this.Keyname = Keyname;
        }
    }
}
